<?php
namespace Admin\Controller;

use Util\AccessController;

require_once(THINK_PATH . "Library/Util/WxRedEnvelope/init.php");

class OrderController extends AccessController {

    public function Index() {

        //快递公司
        $LS1 = Service('Book', 'region');
        $YYpdata = $LS1->find(array('region_name' => '快递公司'));
        $YYsondata = $LS1->query(array('region_fid' => $YYpdata['region_id']));
        $this->KDGS = $YYsondata;

        $this->CSDATAS = json_encode($_GET);
        $this->CSDATA = $_GET;
        $this->ORDER_STATE = json_encode(C('ORDER_STATE_SHOW'));
        $this->ORDER_STATE_SHOW = C('ORDER_STATE_SHOW');

        $where['order_leixing'] = 2;
        $_GET['order_type'] ? $where['order_type'] = $_GET['order_type'] : 1;
        $_GET['order_id'] ? $where['order_id'] = $_GET['order_id'] : 1;
        $_GET['order_code'] ? $where['order_code'] = $_GET['order_code'] : 1;
        $_GET['order_phone'] ? $where['order_phone'] = $_GET['order_phone'] : 1;
        $_GET['order_address_name'] ? $where['order_address_name'] = $_GET['order_address_name'] : 1;
        $_GET['order_express_go'] ? $where['order_express_go'] = $_GET['order_express_go'] : 1;
        $_GET['user_id'] ? $where['ts_order.user_id'] = $_GET['user_id'] : 1;

        $OnePageShow = 5;
        $LS = Service('Order', 'order');
        $count = $LS->count($where);
        $Page = new \Think\NewPage($count, $OnePageShow);
        $show = $Page->show(); // 分页显示输出
        $limit = $Page->limit();
        $order = 'order_start_time DESC';
        $join = 'left join ts_u_user on ts_u_user.user_id = ts_order.user_id ';
        $files = 'ts_order.*,ts_u_user.user_wx_name';
        $DATA = $LS->query($where, $limit, $files, $order, $join);

        if ($_GET['order_type'] == 2) {
            foreach ($DATA as $k => $v) {
                //代发货订单自动拆单操作
                $this->BreakUpOrder($v['order_id']);
                //待发货订单自动挂卖书寄回书籍
                $this->BuyOrderAddHsBook($v['user_id'], $v['order_id']);
            }

        }

        $this->show = $show;
        $this->data = $DATA;

        $this->display();
    }


    /*
     * 修改订单价格
     */
    public function updatecost() {
        $where['order_id'] = $_POST['ID'];
        if (is_numeric($_POST['money']) && $_POST['money'] >= 0) {
            $save['order_money'] = $_POST['money'];
            $LS = Service('Order', 'order');
            $finddata = $LS->find($where);
            if ($finddata) {
                $updateOK = $LS->update($where, $save);
                echo $updateOK;
            } else {
                echo 0;
            }
        } else {
            echo -1;
        }
    }

    //卖书发货操作
    public function FHhanld() {
        $order_id = $_POST['order_id'];
        $where['order_id'] = $order_id;
        $save['order_type'] = 3;
        $save['order_express_go'] = $_POST['KDCODE'];
        $save['order_express_go_name'] = $_POST['KDNAME'];
        $LS = Service('Order', 'order');
        $OK = $LS->update($where, $save);
        //微信通知
        $OrderData = $LS->find($where);
        $ULS = Service('M', 'u_user');
        $Udata = $ULS->find(array('user_id' => $OrderData['user_id']));
        $Send = A('Admin/WxSendMsg');
        $Send->Order_FH_Send($Udata['user_wx'], $OrderData['order_code'], $OrderData['order_money'], $OrderData['order_num'], $OrderData['order_address'], $OrderData['order_address_name'] . ' ' . $OrderData['order_phone'], $order_id);
        echo $OK;

    }

    //出库操作
    public function bookck() {
        $LS = Service('Order', 'wz');
        $OK = $LS->bookck($_POST['WZ_ID'], $_POST['OB_ID'], $this->S('USER_NAME'));
        if ($OK) {
            $ob_data = M('order_book')->where(array('ob_id' => $_POST['OB_ID']))->find();
            $where['order_id'] = $ob_data['order_id'];
            $where['state'] = 0;
            $haveData = M('order_book')->where($where)->select();
            if (!$haveData) {
                $Send = A('Admin/WxSendMsg');
                $Send->CKOrder_SendMsg($ob_data['order_id']);
            }
        }
        echo $OK;
    }

    /*
   * 回收订单状态修改
   */

    public function HSOrderChange() {
        $LS = Service('Order', 'order');
        $Odata = $LS->find(array('order_id' => $_POST['order_id']));
        switch ($_POST['TYPE']) {
            case 2:
                $save['order_express_return_name'] = $_POST['KDNAME'];
                $save['order_express_return'] = $_POST['KDCODE'];
                break;
            case 3:
                break;
            case 4:
                break;
            case 5:
                $user_id = $Odata['user_id'];
                $money = $Odata['order_money'];
                $order_id = $Odata['order_id'];
                $C_LS = Service('Cost', 'cost');
                $C_LS->UserCostAdd($user_id, $money, $order_id);

                break;
        }
        $where['order_id'] = $_POST['order_id'];
        $save['order_type'] = $_POST['TYPE'];
        $OK = $LS->update($where, $save);
        echo $OK;
    }

    /*
     * 回收书籍品相审核
     */
    public function HSOrderBookSH() {
        $where['ob_id'] = $_POST['ob_id'];
        $save['state'] = $_POST['state'];
        $save['info'] = $_POST['info'];
        $LS = Service('Order', 'order_book');
        $OK = $LS->update($where, $save);
        $data = $LS->find($where);
        if ($data) {
            $where1['order_id'] = $data['order_id'];
            $where1['state'] = 0;
            $num = $LS->query($where1);
            if (!$num) {
                $where3['order_id'] = $data['order_id'];
                $where3['state'] = 1;
                $sumMoney = $LS->query($where3, null, 'sum(ob_pay_money) as m');
                $LS1 = Service('Order', 'order');
                $where2['order_id'] = $data['order_id'];
                $save2['order_type'] = 4;
                $save2['order_money'] = $sumMoney[0]['m'];
                $OrderOK = $LS1->update($where2, $save2);
                echo $OrderOK;
            }
        }
        echo $OK;
    }

    /*
    * 根据订单书籍出库
    * 拆分订单
    */
    public function BreakUpOrder($order_id) {
        $LS = Service('M', 'order_book');
        $where['ts_order_book.order_id'] = $order_id;
        $join = 'ts_wz on ts_order_book.book_gg_id = ts_wz.GG_ID';
        $DATA = $LS->join($where, $join, null, null, null, 'ob_id');
        $LS1 = Service('Book', 'sg');
        $CKNUM = array();
        foreach ($DATA as $k => $v) {
            $CKDATA = $LS1->GetWZ1($v['WZ_ID']);
            $CKID = $CKDATA['CK_ID'];
            $DATA[$k]['CK'] = $CKID;
            $DATA[$k]['CK_NAME'] = $CKDATA['P'] . $CKDATA['C'] . $CKDATA['Z'];//仓库名称
            if (!in_array($CKID, $CKNUM)) {
                $CKNUM[] = $CKID;
            }
        }


        $LS2 = Service('M', 'order');
        $orderdata = $LS2->find(array('order_id' => $order_id));
        //出库数等于1数不用拆单
        if (count($CKNUM) != 1) {
            $New_order_ALL = array();//所有订单
            $New_order_book_ALL = array();//所有订单书籍
            foreach ($CKNUM as $k => $v) {
                $new_order_id = $orderdata['order_id'] . rand(0, 99) . '_CF_' . $k;
                $new_order_book_num = 0;//新的书籍本数
                $new_order_book_old_money_all = 0;//新的书籍原定价
                $new_order_book_xs_money_all = 0;//新的书籍销售价
                $new_order_money_all = 0;//订单总价格
                $CKNAME = '';//仓库名称
                foreach ($DATA as $k1 => $v1) {
                    if ($v1['CK'] == $v) {
                        //生成订单书籍详情数据
                        $new_order_book = $v1;
                        $new_order_book['ob_id'] = 'WXOBID_' . uniqid() . rand(0, 99) . '_CF_' . $k1;
                        $new_order_book['order_id'] = $new_order_id;
                        $new_order_book['info'] = $v1['info'] . '《系统备注：拆单自动生成的订单》';
                        $New_order_book_ALL[] = $new_order_book;//
                        //主表信息
                        $new_order_book_num += $v1['ob_num'];//新的书籍本数
                        $new_order_book_old_money_all += ($v1['ob_old_money'] * $v1['ob_num']);//新的书籍原定价
                        $new_order_book_xs_money_all += ($v1['ob_pay_money'] * $v1['ob_num']);//新的书籍销售价
                        $new_order_money_all += $v1['ob_pay_money'];//订单总价格
                        $CKNAME = $v1['CK_NAME'];
                    }
                }
                //主表信息
                $New_order = $orderdata;
                $New_order['order_id'] = $new_order_id;
                $New_order['order_num'] = $new_order_book_num;
                //等于主仓库 邮费 优惠信息不变
                if ($v != C('CK_ID')) {
                    $New_order['order_postage'] = 0;//清除邮费
                }
                //书籍销售价总价
                $New_order['order_book_money'] = $new_order_book_xs_money_all;
                //优惠劵计算比例
                $YHBL = ($New_order['order_book_money']) / $orderdata['order_book_money'];
                //书籍总定价
                $New_order['order_old_money'] = $new_order_book_old_money_all;
                //优惠
                $New_order['order_yh_money'] = $orderdata['order_yh_money'] * $YHBL;
                //余额计算比例
                $YEBL = ($New_order['order_book_money'] + $New_order['order_postage'] - $New_order['order_yh_money']) / $orderdata['order_money'];
                //余额支付
                $New_order['order_ye_pay_money'] = $orderdata['order_ye_pay_money'] * $YEBL;
                $New_order['order_code'] = 'O_' . uniqid() . rand(0, 99);
                $New_order['order_remarks'] = $New_order['order_remarks'] . "《系统备注：拆单自动生成的订单,由$CKNAME 号仓库发货》";

                $New_order['order_money'] = $New_order['order_book_money'] - $New_order['order_yh_money'] + $New_order['order_postage'];
                $New_order_ALL[] = $New_order;
            }

            //数据修改操作
            $LS3 = Service('Order', 'order');
            $OK = $LS3->BreakUpOrder($order_id, $New_order_ALL, $New_order_book_ALL);
            return $OK;
        }
        return 1;

    }


    /*
     *  待发货订单自动挂卖书寄回书籍
     */
    public function BuyOrderAddHsBook($user_id, $order_id) {
        $where['USER_ID'] = $user_id;
        $where['CL_STATE'] = 1;
        $LS = Service('M', 'gd_order_book_view');
        $data = $LS->query($where);
        //有书籍寄回
        if ($data) {
            //生成订单书籍表
            foreach ($data as $k => $v) {
                $save = $v;
                $save['ob_id'] = 'WXJHOBID_' . uniqid() . rand(0, 99);
                $save['order_id'] = $order_id;
                $save['ob_name'] = '随订单书籍寄回书籍《' . $v['ob_name'] . '》';
                $save['info'] = '随订单书籍寄回书籍《' . $v['ob_name'] . '》';
                $save['state'] = 1;
                    //修改用户操作表
                $where1['ID'] = $v['CL_ID'];
                $save1 = $v;
                $save1['NEW_OB_ID'] = $save['ob_id'];
                $save1['STATE'] = 4;
                $OBLS = Service('Order', 'order_book');
                $OBLS->BuyOrderAddHsBook($save,$where1, $save1);
            }
        }
    }

    //微信红包
    public function RedEnvelope() {
        $temp = new \Util\WxRedEnvelope\init();
        /*
        * 设置参数
        * $OPENID 发送红包对象openid
        * $send_name 商户名称
        * $total_amount 付款金额
        * $total_num 红包发放总人数
        * $wishing 红包祝福语
        * $act_name 活动名称
        * $remark 备注
        */
        $temp->set('on7J8046NthBty6rQttBD-uQUSwg', '悦借科技', 100);
        $data = $temp->index();
        dump($data);
    }

    /*
     * 伪删除订单
     */
    public function WDelOrder(){
        $LS = Service('M', 'order');
        $where['order_id'] = $_GET['order_id'];
        $save['order_type'] = -1;
        $OK = $LS->update($where,$save);
        //删除记录
        $LS_LOG = Service('M','order_log');
        $LOG_SAVE['ORDER_ID'] = $_POST['ORDER_ID'];
        $LOG_SAVE['CERATE_TIME'] = date('Y-m-d H:i:s');
        $LOG_SAVE['INFO'] = '后端超级管理员接口操作取消订单';
        $LS_LOG->Add($LOG_SAVE);
        $OK?$this->success('删除成功'):$this->error('删除失败');
    }

}